<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>rgba颜色转换</title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }
        .wrap {
            width: 450px;
            margin: 20px auto;
        }
        .item {
            display: block;
            margin-bottom: 20px;
            line-height: 24px;
            font-size: 18px;
        }
        .label {
            width: 100px;
            height: 24px;
            display: inline-block;
            text-align: justify;
            overflow: hidden;
            vertical-align: middle;
        }
        .label::after {
            content: '';
            display: inline-block;
            width: 100%;
        }
        .input {
            height: 22px;
            width: 300px;
        }
        .button {
            font-size: 18px;
            padding: 2px 4px;
            border-radius: 2px;
            outline: none;
        }
    </style>
</head>
<body>
    <div class="wrap">
        <label class="item"><span class="label">rgb颜色：</span> <input class="input" id="rgba" type="text" placeholder="请输入rgba颜色"></label>
        <label class="item"><span class="label">背景色：</span> <input class="input" id="bkg" type="text" placeholder="默认#ffffff"></label>
        <label class="item"><span class="label">最终颜色：</span> <input class="input" id="result" type="text" placeholder="结果"></label>
        <div><button id="button" class="button">计算</button></div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.2/dist/jquery.min.js" charset="utf-8"></script>
    <script type="text/javascript">
        $(function() {
            function rgbaToColor(color, bkg) {
                let values = color
                    .replace(/rgba?\(/, '')
                    .replace(/\)/, '')
                    .replace(/[\s+]/g, '')
                    .split(',');
                let bkgColor = (bkg || "ffffff")
                    .replace(/^#?/, '')
                    .match(/(\w){2}/g);
                let a = parseFloat(values[3] || 1),
                    r = Math.floor(a * parseInt(values[0]) + (1 - a) * parseInt(bkgColor[0], 16)),
                    g = Math.floor(a * parseInt(values[1]) + (1 - a) * parseInt(bkgColor[1], 16)),
                    b = Math.floor(a * parseInt(values[2]) + (1 - a) * parseInt(bkgColor[2], 16));
                return "#" +
                    ("0" + r.toString(16)).slice(-2) +
                    ("0" + g.toString(16)).slice(-2) +
                    ("0" + b.toString(16)).slice(-2);
            }
            $('#button').click(function() {
                let rgba = $('#rgba').val();
                let bkg = $('#bkg').val();
                let color = rgbaToColor(rgba, bkg);
                $('#result').val(color);
            });
        })
    </script>
</body>
</html>